package GUI;

import java.lang.reflect.InvocationTargetException;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import Game.GameHelper;
import Game.Level;
import Screens.Manager.Screen;

/** GUI manager class
 * @author Aikidos
 */
public class GuiManager {
	
	/** GUI Inventory */
	static private GuiInventory _guiInventory;
	
	/** GUI Player info */
	static private GuiPlayerInfo _guiPlayerInfo;
	
	/** GUI Messenger */
	static private GuiMessenger _guiMessenger;
	
	/** GUI Map */
	static private GuiMap _guiMap;
	
	/** GUI Skills */
	static private GuiSkills _guiSkills;
	
	/** GUI Seller */
	static private GuiSeller _guiSeller;
	
	/** Initialize GUI */
	static public void init(Screen screen, Level level) throws SlickException {
		_guiPlayerInfo = new GuiPlayerInfo(level.getObjectManager().getPlayer());
		_guiInventory = new GuiInventory(screen, level.getObjectManager()
				.getPlayer());
		_guiMessenger = new GuiMessenger();
		_guiMap = new GuiMap(level, _guiMessenger);
		_guiSkills = new GuiSkills(level.getObjectManager().getPlayer());
		_guiSeller = new GuiSeller(level.getObjectManager().getPlayer());
	}
	
	/** Deinitialize GUI */
	static public void deInit() {
		_guiPlayerInfo = null;
		_guiInventory = null;
		_guiMessenger = null;
		_guiMap = null;
		_guiSkills = null;
	}
	
	/** Returns messenger
	 * @return Messenger
	 */
	static public GuiMessenger getMessenger() {
		return _guiMessenger;
	}
	
	/** Returns GUI map
	 * @return GUI map
	 */
	static public GuiMap getGuiMap() {
		return _guiMap;
	}
	
	/** Returns GUI seller
	 * @return GUI seller
	 */
	static public GuiSeller getGuiSeller() {
		return _guiSeller;
	}
	
	/** Returns true if any GUI is open 
	 * @return True if any GUI is open 
	 */
	static public boolean isAnyOpen() {
		return (_guiInventory.isOpen() || _guiMap.isOpen() || _guiSkills.isOpen() || _guiSeller.isOpen());
	}
	
	/** Close all gui */
	static public void closeAll() {
		if (_guiInventory != null) _guiInventory.close();
		if (_guiMap != null) _guiMap.close();
		if (_guiSkills != null) _guiSkills.close();
		if (_guiSeller != null) _guiSeller.close();
	}
	
	static public void update(int delta) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException, SlickException {
		// Get input system
		Input input = GameHelper.getGame().getInput();
		
		// Open/Close inventory
		if (input.isKeyPressed(Input.KEY_TAB)) {
			if (_guiInventory.isOpen())
				_guiInventory.close();
			else
				if (!isAnyOpen()) _guiInventory.open();
		}

		// Open map
		if (input.isKeyPressed(Input.KEY_M)) {
			if (_guiMap.isOpen())
				_guiMap.close();
			else
				if (!isAnyOpen()) _guiMap.open();
		}

		// Open skill menu
		if (input.isKeyPressed(Input.KEY_C)) {
			if (_guiSkills.isOpen())
				_guiSkills.close();
			else
				if (!isAnyOpen()) _guiSkills.open();
		}
		
		// Close all gui
		if (input.isKeyPressed(Input.KEY_ESCAPE)) {
			closeAll();
		}
		
		// Update GUI
		if (_guiInventory != null) _guiInventory.update(delta);
		if (_guiMessenger != null) _guiMessenger.update(delta);
		if (_guiMap != null) _guiMap.update(delta);
		if (_guiSkills != null) _guiSkills.update(delta);
		if (_guiSeller != null) _guiSeller.update(delta);
		
		input.clearKeyPressedRecord();
	}
	
	static public void draw(Graphics g) throws SlickException {
		_guiPlayerInfo.draw(g);
		_guiInventory.draw(g);
		_guiMessenger.draw(g);
		_guiMap.draw(g);
		_guiSkills.draw(g);
		_guiSeller.draw(g);
	}



}
